/**
 * 
 */
package com.nightsoul.groovy.designpattern

import java.util.List

/**
 * @author zj
 *
 */
public class VisitorInGroovy{

	public static void main(def args){
		def picture = new Drawing(shapes:[
		     new Square(width:1), new Circle(radius:1)
		]);
		
		picture.accept({shape->
			println shape.area();
		});
	}
	
}

class Drawing {
	List shapes;
	
	def accept(Closure yield) {
		shapes.each({
			it.accept(yield);
		});
	}
}

class Shape {
	def accept(Closure yield) {
		yield(this);
	}
}

class Square extends Shape {
	def width;
	def area() {
		return width**2;
	}
}
class Circle extends Shape {
	def radius;
	def area() {
		return Math.PI * radius**2;
	}
}
